<html>
<head><meta charset="utf-8"><title>Free function vs macro vs trait method · project-safe-transmute · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/index.html">project-safe-transmute</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Free.20function.20vs.20macro.20vs.20trait.20method.html">Free function vs macro vs trait method</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="215464781"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Free%20function%20vs%20macro%20vs%20trait%20method/near/215464781" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Free.20function.20vs.20macro.20vs.20trait.20method.html#215464781">(Nov 03 2020 at 15:04)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219211">Jack Wrenn</span> <a href="#narrow/stream/216762-project-safe-transmute/topic/summarizing.20for.20lang.20team/near/215386725">said</a>:</p>
<blockquote>
<p><span class="user-mention silent" data-user-id="125270">scottmcm</span>  What's the advantage of a free function, as opposed to a method?</p>
</blockquote>
<p>I think it's less that I feel it should be a free function (though arguably that would be easier to get in the prelude, and is consistent with other <code>mem</code> things) and more that I think it being a method has some potential issues.</p>
<p>For example, having it be a method means that it can be overridden, which is potentially scary in the same way that stabilizing <code>Error::type_id</code> lead to the potential for unsoundness if it's overridden (&lt;<a href="https://groups.google.com/forum/#!topic/rustlang-security-announcements/aZabeCMUv70">https://groups.google.com/forum/#!topic/rustlang-security-announcements/aZabeCMUv70</a>&gt;).  You wouldn't be able to trust, in generic code, that <code>.transmute_into()</code> is actually a transmute, for example.</p>
<p>And as mentioned in the other thread, it seems plausible that we might want to enable overlapping impls for these traits, especially allowing people to implement them for specific special cases and something with a trait bound at the same time.  And doing that means making them <code>#[marker]</code> traits eventually, which is only possible with traits that have no associated items.  (Because then there's no ambiguity in which override to use.)</p>



<a name="215465708"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Free%20function%20vs%20macro%20vs%20trait%20method/near/215465708" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jack Wrenn <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Free.20function.20vs.20macro.20vs.20trait.20method.html#215465708">(Nov 03 2020 at 15:11)</a>:</h4>
<p>Gotcha.</p>
<blockquote>
<p>which is only possible with traits that have no associated items</p>
</blockquote>
<p>IIRC, the discussions around <code>#[marker]</code> include some rumblings that traits with defaulted items should also be <code>#[marker]</code>-able.</p>
<p>Overriding the methods <em>would</em> be problematic, but there there's a note about this in the RFC: <a href="https://github.com/jswrenn/rfcs/blob/safer-transmute/text/0000-safer-transmute.md#limited-stability-guarantees">https://github.com/jswrenn/rfcs/blob/safer-transmute/text/0000-safer-transmute.md#limited-stability-guarantees</a></p>



<a name="215482012"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/216762-project-safe-transmute/topic/Free%20function%20vs%20macro%20vs%20trait%20method/near/215482012" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> scottmcm <a href="https://rust-lang.github.io/zulip_archive/stream/216762-project-safe-transmute/topic/Free.20function.20vs.20macro.20vs.20trait.20method.html#215482012">(Nov 03 2020 at 17:03)</a>:</h4>
<p>Yes, <a href="https://github.com/rust-lang/rust/issues/29864#issuecomment-368478686">I proposed</a> that <code>#[marker]</code> allow defaulted associated items which wouldn't be overridable, but there didn't seem to be appetite for that in the lang team, at least at the time.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>